package edu.hubu.talentmanagementsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.CaseFormat;
import edu.hubu.talentmanagementsystem.mapper.FeedbackTypeMapper;
import edu.hubu.talentmanagementsystem.mapper.context.MapperContext;
import edu.hubu.talentmanagementsystem.model.ext.FeedbackType;
import edu.hubu.talentmanagementsystem.service.IFeedbackTypeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import edu.hubu.talentmanagementsystem.common.EncodeOperation;
import edu.hubu.talentmanagementsystem.common.EncodeParam;

import java.util.List;
import java.util.Optional;

/**
 * @author moonlan
 * date 2021/3/2 下午8:45
 */
@SuppressWarnings(value = "unused")
@Service
@Transactional(rollbackFor = Exception.class)
public class FeedbackTypeServiceImpl extends ServiceImpl<FeedbackTypeMapper, FeedbackType> implements IFeedbackTypeService {

    private final MapperContext context;

    public FeedbackTypeServiceImpl(MapperContext context) {this.context = context;}

    @Override
    public boolean deleteById(Integer id) {
        return context.getFeedbackTypeMapper().deleteById(id) > 0;
    }

    @Override
    @EncodeOperation
    public Optional<FeedbackType> insert(@EncodeParam FeedbackType record) {
        int insert = context.getFeedbackTypeMapper().insert(record);
        if (insert > 0) {
            return Optional.of(context.getFeedbackTypeMapper().selectById(record.primaryKeyValue()));
        }
        return Optional.empty();
    }

    @Override
    public Optional<FeedbackType> selectById(Integer id) {
        QueryWrapper<FeedbackType> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<FeedbackType> lambdaQueryWrapper = getColumnWrapper(wrapper.lambda()).eq(FeedbackType::getFeedbackTypeId, id);
        return Optional.of(context.getFeedbackTypeMapper().selectOne(lambdaQueryWrapper));
    }

    @Override
    public List<FeedbackType> selectAll(String order) {
        QueryWrapper<FeedbackType> wrapper = new QueryWrapper<>();
        if (!StringUtils.hasText(order)) {
            LambdaQueryWrapper<FeedbackType> select = getColumnWrapper(wrapper.lambda());
            return context.getFeedbackTypeMapper().selectList(select);
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        LambdaQueryWrapper<FeedbackType> select = getColumnWrapper(wrapper.lambda());
        return context.getFeedbackTypeMapper().selectList(select);
    }

    @Override
    public Page<FeedbackType> selectAllPaged( Integer currentPage,
             Integer pageSize, String order) {
        QueryWrapper<FeedbackType> wrapper = new QueryWrapper<>();
        Page<FeedbackType> page = new Page<>(currentPage, pageSize);
        if (!StringUtils.hasText(order)) {
            LambdaQueryWrapper<FeedbackType> select = getColumnWrapper(wrapper.lambda());
            return context.getFeedbackTypeMapper().selectPage(page, select);
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        LambdaQueryWrapper<FeedbackType> select = getColumnWrapper(wrapper.lambda());
        return context.getFeedbackTypeMapper().selectPage(page, select);
    }

    private LambdaQueryWrapper<FeedbackType> getColumnWrapper(LambdaQueryWrapper<FeedbackType> wrapper) {
        return wrapper.select(
                FeedbackType::getFeedbackTypeId
                    ,
                FeedbackType::getFeedbackTypeName
        );
    }

    @Override
    public Page<FeedbackType> selectLikePaged( Integer currentPage,
             Integer pageSize, FeedbackType record, String order) {
        Page<FeedbackType> page = new Page<>(currentPage, pageSize);
        QueryWrapper<FeedbackType> wrapper = new QueryWrapper<>();
        if (!StringUtils.hasText(order)) {
            return context.getFeedbackTypeMapper().selectPage(page, getColumnWrapper(getQueryWrapper(wrapper, record)));
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        return context.getFeedbackTypeMapper().selectPage(page, getColumnWrapper(getQueryWrapper(wrapper, record)));
    }

    private LambdaQueryWrapper<FeedbackType> getQueryWrapper(QueryWrapper<FeedbackType>wrapper, FeedbackType record) {
        LambdaQueryWrapper<FeedbackType> lambda = wrapper.lambda();
        if (record.getFeedbackTypeId() != null) {
            lambda = lambda.eq(FeedbackType::getFeedbackTypeId,record.getFeedbackTypeId());
        }
        if (record.getFeedbackTypeName() != null) {
            lambda = lambda.like(FeedbackType::getFeedbackTypeName,record.getFeedbackTypeName ());
        }
        return lambda;
    }

    public int count(FeedbackType record) {
        return context.getFeedbackTypeMapper().selectCount(getQueryWrapper(new QueryWrapper<>(), record));
    }

    @Override
    @EncodeOperation
    public Optional<FeedbackType> updateById(Integer id, @EncodeParam FeedbackType record) {
        record.setFeedbackTypeId(id);
        int update = context.getFeedbackTypeMapper().updateById(record);
        if (update > 0) {
            QueryWrapper<FeedbackType> wrapper = new QueryWrapper<>();
            LambdaQueryWrapper<FeedbackType> lambdaQueryWrapper = getColumnWrapper(wrapper.lambda()).eq(FeedbackType::getFeedbackTypeId, id);
            return Optional.of(context.getFeedbackTypeMapper().selectOne(getColumnWrapper(lambdaQueryWrapper)));
        }

        return Optional.empty();
    }
}
